*********************************************
* Stata v14
********************************************* 

* ssc install estout, replace

*********************************************
*Working directory, change as needed
********************************************* 

cd "~/Dropbox (Samii-research)/Ethnic Quotas/data-analysis/replication-archive"
   
* make sure there is also a folder called "tabs-figs" in the working directory
   
*********************************************
* Clear memory, basic settings
********************************************* 
 	clear
	clear matrix
	clear mata
    set more off
	set scheme s1mono
*********************************************
* Bring in data
********************************************* 

capture log close
log using recog-p2, replace
use recog-data-full-2-2019-01-08, clear

g l_fatalities = log(fatalities+1)
destring exclnow, force replace

*locals
local design_vars postagr recog agreeyrbin ethconf minldrnow cname year cname_count cname_n
local dv_list l_fatalities violence lgdppc v2x_polyarchy v2x_libdem v2x_egaldem v2x_partip v2x_freexp v2x_civlib exclnow ldr_event ldr_trans_irreg
keep `design_vars' `dv_list'
saveold recog-2-repdata, replace

*********************************************
* Labels
********************************************* 

label variable postagr "Post-agreement"
label variable recog "Recognition"

* Note: the minldrnow variable indicates whether the 
* prevailing institutions were adopted under a minority
* or plurality leader (not whether the current head of
* state is minority or majority).

*********************************************
* Set up panel estimation
*********************************************

tsset cname_n year

g cname_count2 = cname_count^2


*********************************************
* Regressions
********************************************* 

replace postagr = 1 if agreeyrbin == 1

*generate vars for DID estimation:
g postagrXethconf = postagr*ethconf
g recogXethconf = recog*ethconf
g postagrXminldrnow = postagr*minldrnow
g recogXminldrnow = recog*minldrnow
g postagrXminldrnowXethconf = postagr*minldrnow*ethconf
g recogXminldrnowXethconf = recog*minldrnow*ethconf

* A dummy table just to get the file started

local yUp violence
eststo clear
eststo: reg `yUp' postagr recog, vce(cluster cname_n)
esttab using "tabs-figs/p2-regs-new-2019-01-08.csv", replace mtitles("Dummy table")

foreach yUp of varlist `dv_list'{

eststo clear

eststo: reg `yUp' postagr recog cname_count cname_count2, vce(cluster cname_n)
eststo: reg `yUp' postagr recog cname_count cname_count2 if ethconf==1, vce(cluster cname_n)
eststo: reg `yUp' postagr recog cname_count cname_count2 if minldrnow == 1, vce(cluster cname_n)
eststo: reg `yUp' postagr recog cname_count cname_count2 if minldrnow == 0, vce(cluster cname_n)
eststo: reg `yUp' postagr recog cname_count cname_count2 if minldrnow == 1 & ethconf == 1, vce(cluster cname_n)
eststo: reg `yUp' postagr recog cname_count if minldrnow == 0 & ethconf == 1, vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year, absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if ethconf == 1,  absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if minldrnow == 1,  absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if minldrnow == 0,  absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if minldrnow == 1 & ethconf==1,  absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if minldrnow == 0 & ethconf==1,  absorb(cname_n) vce(cluster cname_n)
esttab using "tabs-figs/p2-regs-new-2019-01-08.csv", append r2 l cells(`"b(fmt(a2)) ci(fmt(a2) par("" " " ""))"')  nostar keep(postagr recog cname_count  cname_count2 _cons) stats(N r2) mtitles("No FE" "No FE w/ eth. conf." "No FE w/ min. ldr." "No FE w/ plur. ldr." "No FE w/ eth. conf. & min. ldr." "No FE w/ eth. conf. & plur. ldr." "DID w/ all cases" "DID w/ eth. conf." "DID w/ min. ldr." "DID w/ plur. ldr." "DID w/ eth. conf & min. ldr." "DID w/ eth. conf. & plur. ldr.") addn(`"Outcome is "`yUp'"."' "First column is OLS with no fixed effects but controlling for time since onset of violence, second column adds controls for whether a minority group member is leader." "Other columns are difference-in-differences models that add time and country fixed effects." "Robust standard errors clustered by country.")
* esttab using "p2-regs-new-2019-01-08.csv", append r2 l b(3) se(3) nostar keep(postagr recog cname_count  cname_count2 _cons) stats(N r2) mtitles("No FE" "No FE w/ eth. conf." "No FE w/ min. ldr." "No FE w/ plur. ldr." "No FE w/ eth. conf. & min. ldr." "No FE w/ eth. conf. & plur. ldr." "DID w/ all cases" "DID w/ eth. conf." "DID w/ min. ldr." "DID w/ plur. ldr." "DID w/ eth. conf & min. ldr." "DID w/ eth. conf. & plur. ldr.") addn(`"Outcome is "`yUp'"."' "First column is OLS with no fixed effects but controlling for time since onset of violence, second column adds controls for whether a minority group member is leader." "Other columns are difference-in-differences models that add time and country fixed effects." "Robust standard errors clustered by country.")
}

* Regressions to save
* dummy table

local yUp violence
eststo clear
eststo: reg `yUp' postagr recog, vce(cluster cname_n)
esttab using "tabs-figs/p2-regs-for-book-2019-09-14.csv", replace mtitles("Dummy table")

local dv_list l_fatalities violence lgdppc v2x_polyarchy
foreach yUp of varlist `dv_list'{
eststo clear
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year, absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if ethconf == 1,  absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if minldrnow == 1,  absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if minldrnow == 0,  absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if minldrnow == 1 & ethconf==1,  absorb(cname_n) vce(cluster cname_n)
eststo: xi: reghdfe `yUp' postagr recog cname_count cname_count2 i.year if minldrnow == 0 & ethconf==1,  absorb(cname_n) vce(cluster cname_n)
esttab using "tabs-figs/p2-regs-for-book-2019-09-14.csv", append r2 l b(2) se(2) keep(postagr recog) stats(N r2) mtitles("DID w/ all cases" "DID w/ eth. conf." "DID w/ min. ldr." "DID w/ plur. ldr." "DID w/ eth. conf & min. ldr." "DID w/ eth. conf. & plur. ldr.") addn(`"Outcome is "`yUp'"."' "First column is OLS with no fixed effects but controlling for time since onset of violence, second column adds controls for whether a minority group member is leader." "Other columns are difference-in-differences models that add time and country fixed effects." "Robust standard errors clustered by country.")
}


log close


